<?php

namespace app\engine\controller;

use cmf\controller\BaseController;
use cmf\controller\HomeBaseController;
use think\Db;

class ExpenController extends HomeBaseController
{
    /**
     * 加载首页.
     */
    public function index()
    {
        $res = Db::name('expen')->order('date desc')->select()->toArray();
        foreach ($res as $k => &$v) {
            $v['userName'] = Db::name('staff')->where(['id' => $v['uid']])->value('name');
            $v['lname'] = Db::name('staff')->where(['id' => $v['lid']])->value('name');
            $v['count'] = $v['nums'] * $v['price'];
        }
        $by = Db::name('expen')->where(['type' => 1])->sum('nums*price');
        $qz = Db::name('expen')->where(['type' => 2])->sum('nums*price');
        $gs = Db::name('expen')->where(['type' => 3])->sum('nums*price');
        $all = $by + $qz + $gs;
        $this->assign('by', $by);
        $this->assign('qz', $qz);
        $this->assign('gs', $gs);
        $this->assign('all', $all);
        $this->assign('res', $res);
        return $this->fetch();
    }

    /**
     * 加载新增页.
     */
    public function add()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2){
            $this->error('无权限操作');
        }
        $data = Db::name('staff')->field('id,name')->select()->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 执行新增.
     */
    public function doadd()
    {
        $data = $this->request->param();
        if (empty($data['uid']) || empty($data['lid']) || empty($data['unit']) || empty($data['nums']) || empty($data['price']) || empty($data['type']) || empty($data['date'])) {
            $this->error('缺少必要参数');
        }
        $files = $this->request->file('imgs');
        unset($data['imgs']);
        if($files){
            $kps = [];
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['imgs'] = implode(',', $kps);
        }
        $res = Db::name('expen')->insert($data);
        if (!$res) {
            $this->error('操作失败');
            $img = explode(',', $data['imgs']);
            foreach ($img as $k => $v) {
                $v = CMF_ROOT . $v;
                if ($v != CMF_ROOT && file_exists($v)) {
                    unlink($v);
                }
            }
        }
        $this->success('操作成功');
    }

    /**
     * 加载编辑.
     */
    public function see()
    {
        $data = $this->request->param();
        $res = Db::name('expen')->where(['id' => $data['id']])->find();
        $res['userName'] = Db::name('staff')->where(['id' => $res['uid']])->value('name');
        $res['lname'] = Db::name('staff')->where(['id' => $res['lid']])->value('name');
        $res['imgs'] = explode(',', $res['imgs']);
        foreach ($res['imgs'] as $k => &$v) {
            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
        }
        $data = Db::name('staff')->field('id,name')->select()->toArray();
        $this->assign('res', $res);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 执行编辑.
     */
    public function doedit()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2){
            $this->error('无权限操作');
        }
        $data = $this->request->param();
        $id = $data['id'];
        unset($data['id']);
        $files = $this->request->file('imgs');
        if ($files) {
            $check = Db::name('expen')->where(['id' => $id])->find();
            if ($check['imgs']) {
                $img = 'http://' . $_SERVER['SERVER_NAME'] . $check['imgs'];
                if (file_exists($img)) {
                    unlink($img);
                }
            }
            $kps = [];
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['imgs'] = implode(',', $kps);
        }
        $res = Db::name('expen')->where(['id' => $id])->update($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    /**
     * 执行删除.
     */
    public function del()
    {
        $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        if($role == 2){
            $this->error('无权限操作');
        }
        $data = $this->request->param();
        $check = Db::name('expen')->where(['id' => $data['id']])->find('');
        if ($check['imgs']) {
            $img = explode(',', $check['imgs']);
            foreach ($img as $k => $v) {
                $v = CMF_ROOT . $v;
                if ($v != CMF_ROOT && file_exists($v)) {
                    unlink($v);
                }
            }
        }
        $res = Db::name('expen')->where(['id' => $data['id']])->delete();
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }
}